package export.strength
{
    import com.dusk.util.TextUtil;
    import com.dusk.util.UtilBase;
    import com.game.data.vo.EquipmentVO;
    import com.game.data.vo.PlayerDataVO;
    import com.game.event.GameEvent;
    import com.game.manager.EventManager;
    import com.game.manager.NotificationManager;
    import com.game.module.backpack.vo.BackPackEvent;
    import com.game.view.component.icon.RoleEquipIcon;
    
    import config.Config;
    
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    
    public class Strength extends Sprite
    {
        
        public function Strength(param1:PlayerDataVO)
        {
            super();
            TextUtil.redrawTxtOld(this, ["txt_needlh", "txt_success"]);
            cobj = {"allpro": 0};
            gc = Config.getIns();
            player = param1;
            addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
            addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);
        }
        
        public var qhbtn:SimpleButton;
        public var qhmc1:Sprite;
        public var qhmc2:Sprite;
        public var qhmc3:Sprite;
        public var zbmc:Sprite;
        public var luckmc:Sprite;
        public var baodimc:Sprite;
        public var txt_needlh:TextField;
        public var txt_success:TextField;
        private var gc:Config;
        private var curWepon:RoleEquipIcon;
        private var player:PlayerDataVO;
        private var cobj:Object;
        private var tag:Boolean = true;
        private var outList:Array = [];
        
        private function getOutEquip():void
        {
            var _loc1_:* = null;
            outList = [];
            if (zbmc.getChildByName("curzb"))
            {
                _loc1_ = zbmc.getChildByName("curzb") as RoleEquipIcon;
                outList.push({
                    "dequip": _loc1_.getMyEquipObj(),
                    "count": 1
                });
                _loc1_ = null;
            }
            var _loc2_:int = 0;
            while (_loc2_ < 3)
            {
                if (this["qhmc" + (_loc2_ + 1)].getChildByName("curzb"))
                {
                    _loc1_ = this["qhmc" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
                    outList.push({
                        "dequip": _loc1_.getMyEquipObj(),
                        "count": 1
                    });
                    _loc1_ = null;
                }
                _loc2_++;
            }
            if (luckmc.getChildByName("curzb"))
            {
                _loc1_ = luckmc.getChildByName("curzb") as RoleEquipIcon;
                outList.push({
                    "dequip": _loc1_.getMyEquipObj(),
                    "count": 1
                });
                _loc1_ = null;
            }
            if (baodimc.getChildByName("curzb"))
            {
                _loc1_ = baodimc.getChildByName("curzb") as RoleEquipIcon;
                outList.push({
                    "dequip": _loc1_.getMyEquipObj(),
                    "count": 1
                });
                _loc1_ = null;
            }
        }
        
        private function judgeallpro():void
        {
            if (cobj.allpro > 1)
            {
                cobj = UtilBase.clone(cobj);
                cobj.allpro = 1;
            }
        }
        
        private function changeLuck():void
        {
            var _loc1_:* = null;
            var _loc2_:int = 0;
            var _loc3_:Number = Number(NaN);
            var _loc4_:Number = Number(NaN);
            trace("--changeLuck--");
            cobj = UtilBase.clone(cobj);
            cobj.allpro = 0;
            if (curWepon)
            {
                _loc2_ = 1;
                while (_loc2_ <= 3)
                {
                    if (this["qhmc" + _loc2_].getChildByName("curzb") != null)
                    {
                        _loc1_ = this["qhmc" + _loc2_].getChildByName("curzb");
                        _loc3_ = Number(gc.allEquip.getStrengthNumber(_loc1_.getMyEquipObj(), curWepon.getEquipVO()));
                        cobj.allpro += _loc3_;
                        judgeallpro();
                        txt_success.text = Math.floor(Number(cobj.allpro) * 100) + "%";
                    }
                    _loc2_++;
                }
                if (luckmc.getChildByName("curzb") != null)
                {
                    _loc4_ = Number(cobj.allpro) * 0.25;
                    cobj = UtilBase.clone(cobj);
                    cobj.allpro += _loc4_;
                    judgeallpro();
                    txt_success.text = Math.floor(Number(cobj.allpro) * 100) + "%";
                }
            }
            else
            {
                txt_success.text = Math.floor(Number(cobj.allpro) * 100) + "%";
            }
        }
        
        private function removeEquipFormBack(param1:EquipmentVO, param2:int):void
        {
            var _loc3_:int = 0;
            if (param2 == 1)
            {
                if (param1.num > 1)
                {
                    param1.modifyNum(-1);
                }
                else
                {
                    _loc3_ = int(player.bagEquipList.indexOf(param1));
                    if (_loc3_ != -1)
                    {
                        player.bagEquipList.splice(_loc3_, 1);
                    }
                }
            }
            else if (param2 == 2)
            {
                if (param1.num > 1)
                {
                    param1.modifyNum(-1);
                }
                else
                {
                    _loc3_ = int(player.bagStuffList.indexOf(param1));
                    if (_loc3_ != -1)
                    {
                        player.bagStuffList.splice(_loc3_, 1);
                    }
                }
            }
            else if (param2 == 3)
            {
                if (param1.num > 1)
                {
                    param1.modifyNum(-1);
                }
                else
                {
                    _loc3_ = int(player.bugConsumeList.indexOf(param1));
                    if (_loc3_ != -1)
                    {
                        player.bugConsumeList.splice(_loc3_, 1);
                    }
                }
            }
        }
        
        private function findIfQHS():Boolean
        {
            var _loc1_:int = 0;
            while (_loc1_ < 3)
            {
                if (this["qhmc" + (_loc1_ + 1)].getChildByName("curzb") != null)
                {
                    return true;
                }
                _loc1_++;
            }
            return false;
        }
        
        private function doStrength():void
        {
            var _loc1_:Number;
            if (!curWepon)
            {
                NotificationManager.alert("请放入装备！");
                return;
            }
            _loc1_ = gc.configManager.equipProxy.getNeedStrengthLh(curWepon.getEquipVO());
            if (!findIfQHS())
            {
                NotificationManager.alert("请放入强化石！");
                return;
            }
            if (player.lhValue < _loc1_)
            {
                NotificationManager.alert("灵魂不足！");
                return;
            }
            player.reduceLhValue(_loc1_)
            StrengthEquipment(parent).setTxtlh();
            afterReadStore();
            clearAllThingExceptWeapen();
            curWepon = null;
        }
        
        private function afterReadStore():void
        {
            trace("cobj.allpro:" + cobj.allpro);
            if (Math.random() < cobj.allpro)
            {
                trace("强化成功");
                NotificationManager.alert("强化成功");
                curWepon.getEquipVO().upStrengthValue(1);
            }
            else
            {
                trace("强化失败");
                NotificationManager.alert("强化失败");
                if (curWepon.getEquipVO().getStrengthLevel() >= 3)
                {
                    if (baodimc.getChildByName("curzb") == null)
                    {
                        trace("降级");
                        curWepon.getEquipVO().upStrengthValue(-1);
                    }
                }
            }
            trace("等级:" + curWepon.getEquipVO().getStrengthLevel());
        }
        
        private function clearAllThingExceptWeapen(param1:Boolean = true):void
        {
            var _loc2_:* = null;
            var _loc3_:* = null;
            outList = [];
            curWepon = null;
            cobj.allpro = 0;
            if (param1)
            {
                if (zbmc.getChildByName("curzb"))
                {
                    _loc2_ = zbmc.getChildByName("curzb") as RoleEquipIcon;
                    zbmc.removeChild(_loc2_);
                    if (_loc2_.getMyEquipObj().type == "zbcb" || _loc2_.getMyEquipObj().type == "zbsz")
                    {
                        player.bugConsumeList.push(_loc2_.getMyEquipObj());
                    }
                    else
                    {
                        player.bagEquipList.push(_loc2_.getMyEquipObj());
                    }
                    _loc2_ = null;
                }
            }
            else if (zbmc.getChildByName("curzb"))
            {
                _loc2_ = zbmc.getChildByName("curzb") as RoleEquipIcon;
                zbmc.removeChild(_loc2_);
                _loc2_ = null;
            }
            var _loc4_:* = "";
            var _loc5_:Date;
            var _loc6_:int = (_loc5_ = new Date()).getDate();
            var _loc7_:int = _loc5_.getHours();
            var _loc8_:int = _loc5_.getMinutes();
            var _loc9_:String = _loc6_ + "-" + _loc7_ + "-" + _loc8_;
            var _loc10_:int = 0;
            while (_loc10_ < 3)
            {
                if (this["qhmc" + (_loc10_ + 1)].getChildByName("curzb"))
                {
                    _loc2_ = this["qhmc" + (_loc10_ + 1)].getChildByName("curzb") as RoleEquipIcon;
                    _loc3_ = _loc2_.getMyEquipObj().equipId;
                    if (_loc3_.indexOf("wpqhs") != -1)
                    {
                        _loc4_ += _loc3_.substr(5, 1);
                        if (_loc10_ < 2)
                        {
                            _loc4_ += ",";
                        }
                    }
                    this["qhmc" + (_loc10_ + 1)].removeChild(_loc2_);
                    _loc2_ = null;
                }
                _loc10_++;
            }
            /*if (_loc4_ != "")
            {
                if (gc.qhsInfo.length >= 10)
                {
                    gc.qhsInfo.shift();
                }
                gc.qhsInfo.push(_loc9_ + "|" + _loc4_ + "|1");
            }*/
            if (luckmc.getChildByName("curzb"))
            {
                _loc2_ = luckmc.getChildByName("curzb") as RoleEquipIcon;
                luckmc.removeChild(_loc2_);
                _loc2_ = null;
            }
            if (baodimc.getChildByName("curzb"))
            {
                _loc2_ = baodimc.getChildByName("curzb") as RoleEquipIcon;
                baodimc.removeChild(_loc2_);
                _loc2_ = null;
            }
            EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
        }
        
        private function removedSelf():void
        {
            var _loc1_:* = null;
            if (zbmc.getChildByName("curzb"))
            {
                _loc1_ = zbmc.getChildByName("curzb") as RoleEquipIcon;
                zbmc.removeChild(_loc1_);
                if (_loc1_.getMyEquipObj().type == "zbcb" || _loc1_.getMyEquipObj().type == "zbsz")
                {
                    player.bugConsumeList.push(_loc1_.getMyEquipObj());
                }
                else
                {
                    player.bagEquipList.push(_loc1_.getMyEquipObj());
                }
                _loc1_ = null;
            }
            var _loc2_:int = 0;
            while (_loc2_ < 3)
            {
                if (this["qhmc" + (_loc2_ + 1)].getChildByName("curzb"))
                {
                    _loc1_ = this["qhmc" + (_loc2_ + 1)].getChildByName("curzb") as RoleEquipIcon;
                    this["qhmc" + (_loc2_ + 1)].removeChild(_loc1_);
                    gc.putQhsInBackPack(player, _loc1_.getMyEquipObj().equipId);
                    _loc1_ = null;
                }
                _loc2_++;
            }
            if (luckmc.getChildByName("curzb"))
            {
                _loc1_ = luckmc.getChildByName("curzb") as RoleEquipIcon;
                luckmc.removeChild(_loc1_);
                gc.putQhsInBackPack(player, _loc1_.getMyEquipObj().equipId);
                _loc1_ = null;
            }
            if (baodimc.getChildByName("curzb"))
            {
                _loc1_ = baodimc.getChildByName("curzb") as RoleEquipIcon;
                baodimc.removeChild(_loc1_);
                gc.putQhsInBackPack(player, _loc1_.getMyEquipObj().equipId);
                _loc1_ = null;
            }
            EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
        }
        
        private function added(param1:Event):void
        {
            if (tag)
            {
                tag = false;
                outList = [];
                gc.eventManager.addEventListener("SimpleClick", receiveObj);
                qhmc1.addEventListener(MouseEvent.CLICK, onClick);
                qhmc2.addEventListener(MouseEvent.CLICK, onClick);
                qhmc3.addEventListener(MouseEvent.CLICK, onClick);
                zbmc.addEventListener(MouseEvent.CLICK, onClick);
                luckmc.addEventListener(MouseEvent.CLICK, onClick);
                baodimc.addEventListener(MouseEvent.CLICK, onClick);
                qhbtn.addEventListener(MouseEvent.CLICK, qhClick);
            }
        }
        
        private function removed(param1:Event):void
        {
            outList = [];
            gc.eventManager.removeEventListener("SimpleClick", receiveObj);
            qhmc1.removeEventListener(MouseEvent.CLICK, onClick);
            qhmc2.removeEventListener(MouseEvent.CLICK, onClick);
            qhmc3.removeEventListener(MouseEvent.CLICK, onClick);
            zbmc.removeEventListener(MouseEvent.CLICK, onClick);
            luckmc.removeEventListener(MouseEvent.CLICK, onClick);
            baodimc.removeEventListener(MouseEvent.CLICK, onClick);
            qhbtn.removeEventListener(MouseEvent.CLICK, qhClick);
            removedSelf();
        }
        
        private function receiveObj(param1:GameEvent):void
        {
            var _loc4_:Number = NaN;
            var _loc2_:* = null;
            trace("receiveObj");
            var _loc3_:RoleEquipIcon = param1.data[0] as RoleEquipIcon;
            trace("sobj.getMyEquipObj().type:" + _loc3_.getEquipVO().type);
            if (_loc3_.getEquipVO().type == "zbtx")
            {
                NotificationManager.alert("该装备不能强化");
                return;
            }
            if (_loc3_.getEquipVO().getStrengthLevel() >= 7)
            {
                NotificationManager.alert("该装备强化等级已达上限");
                return;
            }
            if (_loc3_.getEquipVO().equipId == "hy" || _loc3_.getEquipVO().equipId == "_dzj" || _loc3_.getEquipVO().equipId == "dzjj" || _loc3_.getEquipVO().quality == "神 器")
            {
                NotificationManager.alert("该装备暂时不能强化");
                return;
            }
            switch (_loc3_.getEquipVO().type)
            {
                case "zbwq":
                case "zbfj":
                case "zbsp":
                case "zbsz":
                case "":
                    if (zbmc.getChildByName("curzb") == null)
                    {
                        zbmc.addChild(_loc3_);
                    }
                    else
                    {
                        _loc2_ = zbmc.getChildByName("curzb") as RoleEquipIcon;
                        zbmc.removeChild(_loc2_);
                        if (_loc2_.getMyEquipObj().type == "zbsz")
                        {
                            player.bugConsumeList.push(_loc2_.getMyEquipObj());
                        }
                        else
                        {
                            player.bagEquipList.push(_loc2_.getMyEquipObj());
                        }
                        _loc2_ = null;
                        zbmc.addChild(_loc3_);
                    }
                    curWepon = null;
                    curWepon = _loc3_;
                    if (_loc3_.getEquipVO().type == "zbsz")
                    {
                        removeEquipFormBack(_loc3_.getEquipVO(), 3);
                    }
                    else
                    {
                        removeEquipFormBack(_loc3_.getEquipVO(), 1);
                    }
                    changeLuck();
                    _loc4_ = gc.configManager.equipProxy.getNeedStrengthLh(curWepon.getEquipVO());
                    txt_needlh.text = String(_loc4_);
                    break;
                case "wpqhs":
                    if (qhmc1.numChildren < 2)
                    {
                        qhmc1.addChild(_loc3_);
                        removeEquipFormBack(_loc3_.getEquipVO(), 2);
                        changeLuck();
                        break;
                    }
                    if (qhmc2.numChildren < 2)
                    {
                        qhmc2.addChild(_loc3_);
                        removeEquipFormBack(_loc3_.getEquipVO(), 2);
                        changeLuck();
                        break;
                    }
                    if (qhmc3.numChildren < 2)
                    {
                        qhmc3.addChild(_loc3_);
                        removeEquipFormBack(_loc3_.getEquipVO(), 2);
                        changeLuck();
                        break;
                    }
                    break;
                case "zbwp":
                    if (_loc3_.getEquipVO().equipId == "wpxyf")
                    {
                        if (luckmc.getChildByName("curzb") == null)
                        {
                            luckmc.addChild(_loc3_);
                            changeLuck();
                            removeEquipFormBack(_loc3_.getEquipVO(), 2);
                            break;
                        }
                        return;
                    }
                    if (_loc3_.getEquipVO().equipId == "wpbdf")
                    {
                        if (baodimc.getChildByName("curzb") == null)
                        {
                            baodimc.addChild(_loc3_);
                            removeEquipFormBack(_loc3_.getEquipVO(), 2);
                            break;
                        }
                        return;
                    }
                    break;
            }
            getOutEquip();
            EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
        }
        
        private function onClick(param1:MouseEvent):void
        {
            var _loc2_:* = null;
            if (param1.currentTarget.getChildByName("curzb") != null)
            {
                _loc2_ = param1.currentTarget.getChildByName("curzb") as RoleEquipIcon;
                param1.currentTarget.removeChild(_loc2_);
                switch (_loc2_.getMyEquipObj().type)
                {
                    case "zbwq":
                    case "zbfj":
                    case "zbsp":
                    case "":
                        player.bagEquipList.push(_loc2_.getMyEquipObj());
                        changeLuck();
                        curWepon = null;
                        break;
                    case "zbsz":
                        if (_loc2_.getMyEquipObj().ename.indexOf("装") != -1)
                        {
                            player.bugConsumeList.push(_loc2_.getMyEquipObj());
                            changeLuck();
                            curWepon = null;
                            break;
                        }
                        break;
                    case "wpqhs":
                        if (_loc2_.getMyEquipObj().ename.indexOf("强化石") != -1)
                        {
                            gc.putQhsInBackPack(player, _loc2_.getMyEquipObj().equipId);
                            if (curWepon != null)
                            {
                                changeLuck();
                                break;
                            }
                            break;
                        }
                        break;
                    case "zbwp":
                        if (_loc2_.getMyEquipObj().ename.indexOf("幸运") != -1 || _loc2_.getMyEquipObj().ename.indexOf("神恩") != -1)
                        {
                            if (_loc2_.getMyEquipObj().equipId == "wpxyf")
                            {
                                gc.putQhsInBackPack(player, _loc2_.getMyEquipObj().equipId);
                                changeLuck();
                                break;
                            }
                            if (_loc2_.getMyEquipObj().equipId == "wpbdf")
                            {
                                gc.putQhsInBackPack(player, _loc2_.getMyEquipObj().equipId);
                                break;
                            }
                            break;
                        }
                }
                getOutEquip();
                EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
                _loc2_ = null;
            }
        }
        
        private function qhClick(param1:MouseEvent):void
        {
            doStrength();
        }
    }
}
